﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LaMalleAJeux.Models;
using LaMalleAJeux.Properties;

namespace LaMalleAJeux.Controllers
{ 
    public class GamesController : Controller
    {
        private ProjectDbContext db = new ProjectDbContext();

        //
        // GET: /Games/

        public ViewResult Index()
        {
            ViewBag.Title = Resources.Games;

            return View(db.Games.ToList());
        }

        //
        // GET: /Games/Details/5

        public ActionResult Details(int id)
        {
            Game game = db.Games.Find(id);
            if (game == null)
            {
                return HttpNotFound();
            }
            return View(game);
        }

        //
        // GET: /Games/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Games/Create

        [HttpPost]
        public ActionResult Create(Game game)
        {
            if (ModelState.IsValid)
            {
                game.Rating = 50;
                game.IsNew = true;
                db.Games.Add(game);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(game);
        }
        
        //
        // GET: /Games/Edit/5
 
        public ActionResult Edit(int id)
        {
            Game game = db.Games.Find(id);
            if (game == null)
            {
                return HttpNotFound();
            }
            return View(game);
        }

        //
        // POST: /Games/Edit/5

        [HttpPost]
        public ActionResult Edit(Game game)
        {
            if (ModelState.IsValid)
            {
                db.Entry(game).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(game);
        }

        //
        // GET: /Games/Delete/5
 
        public ActionResult Delete(int id)
        {
            Game game = db.Games.Find(id);
            if (game == null)
            {
                return HttpNotFound();
            }
            return View(game);
        }

        //
        // POST: /Games/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Game game = db.Games.Find(id);
            db.Games.Remove(game);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult SearchIndex(string searchString)
        {
            IEnumerable<Game> games = from m in db.Games select m;

            if (!String.IsNullOrEmpty(searchString))
            {
                games = games.Where(s => s.Name.Contains(searchString));
            }

           return View("Index", games);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}